package com.survey.service.impl;

import com.survey.dao.mapper.QuestionBodyMapper;
import com.survey.dao.mapper.QuestionMapper;
import com.survey.dao.mapper.ResultMapper;
import com.survey.dao.mapper.SurveyMapper;
import com.survey.pojo.data.QuestionBodyDO;
import com.survey.pojo.data.QuestionDO;
import com.survey.pojo.data.ResultDO;
import com.survey.pojo.data.SurveyDO;
import com.survey.pojo.vo.*;
import com.survey.service.ResultService;

import com.survey.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * <br/>
 * Created by survey on 2018/08/04
 */
@Service("resultService")
public class ResultServiceImpl implements ResultService {

    private final static Logger LOG = LoggerFactory.getLogger(ResultServiceImpl.class);

    @Resource
    private ResultMapper resultMapper;
    @Autowired
    private SurveyMapper surveyMapper;
    @Autowired
    private QuestionMapper questionMapper;
    @Autowired
    private QuestionBodyMapper questionBodyMapper;

    @Override
    public Result addResult(ResultVO resultVO) throws Exception {
        return null;
    }

    @Override
    public Result updateResult(ResultVO resultVO) throws Exception {
        return null;
    }

    @Override
    public Result findDetailResult(ResultVO resultVO) throws Exception {
        return null;
    }

    @Override
    public Result listResult(ResultVO resultVO) throws Exception {
        return null;
    }

    @Override
    public Result listResultPage(ResultVO resultVO) throws Exception {
        return null;
    }

    @Override
    public Result countResult(ResultVO resultVO) throws Exception {
        return null;
    }


    /**
     * 根据试卷id获得试卷信息
     *
     * @param sid 问卷id
     * @return
     */
    @Override
    public List<JJOption> getQuestionInfo(Integer sid) {//1
        List<StatisData> statisDataS=new ArrayList<>();
        QuestionVO questionVO = new QuestionVO();
        questionVO.setFkSurvey(sid);
        List<QuestionDO> questionDOS = questionMapper.listQuestion(questionVO);
        ArrayList<JJOption> jjOptions = new ArrayList<>();
        for (QuestionDO questionDO : questionDOS) {
            StatisData statisData=new StatisData();
            JJOption jjOption = new JJOption();
            //每个问题的题目
            String head = questionDO.getHead();
            jjOption.setQuestionName(head);//封装题目名字
            //------------------------------------------------------
            Integer fkBody = questionDO.getFkBody();
            QuestionBodyVO questionBodyVO = new QuestionBodyVO();
            questionBodyVO.setId(fkBody);//1
            QuestionBodyDO detailQuestionBody = questionBodyMapper.findDetailQuestionBody(questionBodyVO);
            String optiona = detailQuestionBody.getOptiona();
            String optionb = detailQuestionBody.getOptionb();
            String optionc = detailQuestionBody.getOptionc();
            String optiond = detailQuestionBody.getOptiond();
            jjOption.setOptionNamea(optiona);
            jjOption.setOptionNameb(optionb);
            jjOption.setOptionNamec(optionc);
            jjOption.setOptionNamed(optiond);
            //题目的id
            Integer id = detailQuestionBody.getId();
            ResultVO resultVO = new ResultVO();
            resultVO.setFkSid(sid);
            resultVO.setFkQid(questionDO.getId());
            ResultDO detailResult = resultMapper.findDetailResult1(resultVO);
            if (detailResult!=null){
                Integer counta = detailResult.getCounta();
                Integer countb = detailResult.getCountb();
                Integer countc = detailResult.getCountc();
                Integer countd = detailResult.getCountd();
                jjOption.setCountA(counta);
                jjOption.setCountB(countb);
                jjOption.setCountC(countc);
                jjOption.setCountD(countd);
                int totalCount;
                totalCount = counta + countb + countc + countd;
                jjOption.setTotalCount(totalCount);
                DecimalFormat    df   = new DecimalFormat("######0.00");
                double perCountA = 1.0 * counta / totalCount * 100;
                double perCountB = 1.0 * countb / totalCount * 100;
                double perCountC = 1.0 * countc / totalCount * 100;
                double perCountD = 1.0 * countd / totalCount * 100;
                jjOption.setPercountA(Double.parseDouble(df.format(perCountA)));
                jjOption.setPercountB(Double.parseDouble(df.format(perCountB)));
                jjOption.setPercountC(Double.parseDouble(df.format(perCountC)));
                jjOption.setPercountD(Double.parseDouble(df.format(perCountD)));
                jjOptions.add(jjOption);
            }else {
                Integer counta = 0;
                Integer countb = 0;
                Integer countc = 0;
                Integer countd = 0;
                jjOption.setCountA(counta);
                jjOption.setCountB(countb);
                jjOption.setCountC(countc);
                jjOption.setCountD(countd);
                int totalCount=0;
                jjOption.setTotalCount(totalCount);
                double perCountA = 0;
                double perCountB = 0;
                double perCountC = 0;
                double perCountD = 0;
                jjOption.setPercountA(perCountA);
                jjOption.setPercountB(perCountB);
                jjOption.setPercountC(perCountC);
                jjOption.setPercountD(perCountD);
                jjOptions.add(jjOption);
            }

            /*statisData.setJjQuestions(jjOptions);
            statisDataS.add(statisData);*/
        }
        return jjOptions;
    }

    /**
     * 根据试卷id获得统计信息
     *
     * @param eid 问卷id
     * @return
     */
    @Override
    public StatisticsData getStatisticsData(Integer eid) {
        SurveyVO surveyVO = new SurveyVO();
        ArrayList<AnswerVO> answerVOS = new ArrayList<>();
        surveyVO.setId(eid);
        List<SurveyDO> surveyDOS = surveyMapper.listSurvey(surveyVO);
        StatisticsData data = new StatisticsData();
        if (!surveyDOS.isEmpty()) {
            //1.封装问卷标题
            data.setSurveyTitle(surveyDOS.get(0).getName());
        }
        QuestionVO questionVO = new QuestionVO();
        questionVO.setFkSurvey(eid);
        List<QuestionDO> questionDOS = questionMapper.listQuestion(questionVO);
        ArrayList<String> heads = new ArrayList<>();
        ArrayList<QuestionBodyDO> questionBodyDOArrayList = new ArrayList<>();
        for (QuestionDO questionDO : questionDOS) {
            //每个问题的题目
            String head = questionDO.getHead();
            Integer fkBody = questionDO.getFkBody();
            QuestionBodyVO questionBodyVO = new QuestionBodyVO();
            questionBodyVO.setId(fkBody);
            List<QuestionBodyDO> questionBodyDOS = questionBodyMapper.listQuestionBody(questionBodyVO);
            for (QuestionBodyDO questionBodyDO : questionBodyDOS) {
                questionBodyDOArrayList.add(questionBodyDO);
                questionBodyDO.getOptiona();
                Integer id1 = questionBodyDO.getId();
                //封装结果集
                ResultVO resultVO = new ResultVO();
                resultVO.setFkSid(eid);
                resultVO.setFkQid(id1);
                List<ResultDO> resultDOS = resultMapper.listResult1(resultVO);
                int totalCount = 0;
                for (ResultDO resultDO : resultDOS) {
                    AnswerVO answerVO = new AnswerVO();
                    Integer counta = resultDO.getCounta();
                    Integer countb = resultDO.getCountb();
                    Integer countc = resultDO.getCountc();
                    Integer countd = resultDO.getCountd();
                    totalCount = counta + countb + countc + countd;
                    //4.封装每道题目的总回答数
                    answerVO.setTotalCount(totalCount);
                    //5.封装选每个选项的个个数
                    answerVO.setCountA(counta);
                    answerVO.setCountB(countb);
                    answerVO.setCountC(countc);
                    answerVO.setCountD(countd);
                    //6.封装选每个选项的百分比
                    double perCountA = 1.0 * counta / totalCount * 100;
                    double perCountB = 1.0 * countb / totalCount * 100;
                    double perCountC = 1.0 * countc / totalCount * 100;
                    double perCountD = 1.0 * countd / totalCount * 100;
                    answerVO.setPercountA(perCountA);
                    answerVO.setPercountB(perCountB);
                    answerVO.setPercountC(perCountC);
                    answerVO.setPercountD(perCountD);
                    answerVOS.add(answerVO);
                }
                data.setAnswerVOS(answerVOS);
            }
            //3.封装每个问题的选项
            heads.add(head);
        }
        data.setQuestionBodyDOS(questionBodyDOArrayList);
        //2.封装每个问题的题目
        data.setQuestionTitle(heads);
        return data;
    }

    @Override
    public Result deleteResult(ResultVO resultVO) throws Exception {
        return null;
    }
}